//
//  NewsPindao.m
//  News
//
//  Created by 李旭阳 on 15/12/29.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "NewsPindao.h"
#import <objc/runtime.h>

@implementation NewsPindao

+(instancetype)pindaoWithDict:(NSDictionary *)dict{
    
    NewsPindao *newsPindao = [[NewsPindao alloc] init];
    
    NSArray *properties = [newsPindao getProperties];
    
    //遍历属性
    [properties enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *property = obj;
        
        if (dict[property]) {
            [newsPindao setValue:dict[property] forKey:property];
        }
    }];
    
    return newsPindao;
}


#pragma mark 获得类的所有属性 NSArray 
-(NSArray *)getProperties{
    
    unsigned int count;
    
    objc_property_t *properties = class_copyPropertyList(self.class, &count);
    
    NSMutableArray *array = [NSMutableArray array];
    
    for (int i = 0; i < count; ++i) {
        
        objc_property_t pro = properties[i];
        
        const char *name = property_getName(pro);
        
        NSString *property = [[NSString alloc] initWithCString:name encoding:NSUTF8StringEncoding];
        
        [array addObject: property];
    }
    
    return array;
}

@end
